package thinkinjava1.ch10;

import thinkinjava1.ch08.StrSortVector;

import java.io.File;

/**
 * SortedDirList 顺序目录列表
 * <p>
 * author: 顾博君 <br>
 * time:   2017/2/23 11:27 <br>
 * e-mail: gubojun@csii.com.cn <br>
 * </p>
 */
public class SortedDirList {
    private File path;
    private String[] list;

    public SortedDirList(final String afn) {
        path = new File("src/thinkinjava1.ch08");
        if (afn == null)
            list = path.list();
        else
            list = path.list((dir, name) -> {
                String f = new File(name).getName();
                return f.indexOf(afn) != -1;
            });
        sort();
    }

    void print() {
        for (int i = 0; i < list.length; i++)
            System.out.println(list[i]);
    }

    private void sort() {
        StrSortVector sv = new StrSortVector();
        for (int i = 0; i < list.length; i++)
            sv.addElement(list[i]);
// The first time an element is pulled from
        // the StrSortVector the list is sorted:
        for (int i = 0; i < list.length; i++)
            list[i] = sv.elementAt(i);
    }

    // Test it:
    public static void main(String[] args) {
        SortedDirList sd;
        if (args.length == 0)
            sd = new SortedDirList(null);
        else
            sd = new SortedDirList(args[0]);
        sd.print();
    }
}
